package chapter06

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test13_Trait
 * Package: chapter06
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 *          Date: 2023-06-23
 *          Time: 12:27
 */

object Test13_Trait {
  def main(args: Array[String]): Unit = {
    val stu = new Student13
    stu.sayHello()
    stu.study()
    stu.dating()
    stu.play()

  }
}
//定义父类
class Person13 {
  val name: String = "person"
  var age: Int = 18
  def sayHello(): Unit = {
    println("hello from: " + name)
  }
}
//定义一个特质 相当于Java的接口 具体的对象具有的一些特质
//人 到学生 到青年人具备的特质 定义规则方法和抽象类差不多
trait Yong {
  //可以定义抽象和非抽象的
  var age: Int
  val name: String = "young"

  def play(): Unit ={
    println("yong is playing")
  }
  def dating():Unit
}

//定义一个子类
class Student13 extends Person13 with Yong{

  //父类当中有name 特质当中也有name 继承了父类 实现了特质 相同的属性就冲突了
  //编译器没有报错 但是运行错误 解决办法是 在子类中 重新申明冲突的属性
  override val name: String = "student"
  //同理  父类有age 特质有抽象的age 所以补充些也没问题

  def dating(): Unit = println(s"student ${name} is dating ${age} 555")

  def study()=println(s"student $name is studying")


  //重写父类方法
  override def sayHello(): Unit = {

    super.sayHello()
    println(s"hello from: student $name")
  }

}